<template>
	<sw-form-frame v-model="visible" :title="formName" :type="formType" width="560px">
		<el-form ref="form" size="medium" :model="formData" :rules="formRules" label-width="80px">
			<el-row :gutter="32">
				<el-col :span="16">
					<el-form-item label="上级菜单" prop="pid" label-width="80px">
						<sw-search-tree-select v-model="formData.pid" v-bind="treeSelectConfig" :tree-data="menuTreeList" :disabled="isDisabled" />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label-width="0">
						<el-radio-group v-model="isButton" v-removeAriaHidden>
							<el-radio label="1">菜单</el-radio>
							<el-radio label="2">按钮</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="菜单名称" prop="name">
						<el-input type="input" v-model="formData.name" maxlength="64" show-word-limit></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="isButton === '1'">
					<el-form-item label="是否业务菜单" label-width="100px">
						<el-radio-group v-model="formData.isBusi" v-removeAriaHidden>
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="isButton === '1'">
					<el-form-item label="是否开发菜单" label-width="100px">
						<el-radio-group v-model="formData.isDev" v-removeAriaHidden>
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="isButton === '1'">
					<el-form-item label="是否公共菜单" label-width="100px">
						<el-radio-group v-model="formData.isPublic" v-removeAriaHidden>
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="是否激活" :label-width="isButton === '1' ? '100px' : '80px'">
						<el-radio-group v-model="formData.del" v-removeAriaHidden>
							<el-radio label="1">是</el-radio>
							<el-radio label="2">否</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="菜单排序">
						<el-input-number v-model="formData.sort" controls-position="right" :min="1"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="isButton === '1'">
					<el-form-item label="菜单图标">
						<select-icon v-model="formData.icon"></select-icon>
					</el-form-item>
				</el-col>
			</el-row>
			<el-col :span="24" v-if="isButton === '1'">
				<el-form-item label="菜单路径" prop="url" label-width="80px">
					<el-input type="input" v-model="formData.url"></el-input>
				</el-form-item>
			</el-col>
			<el-col :span="24">
				<el-form-item label="菜单描述" prop="content" label-width="80px">
					<el-input type="textarea" v-model="formData.content"></el-input>
				</el-form-item>
			</el-col>
		</el-form>
		<template slot="footer">
			<el-button size="medium" type="primary" @click="handleSubmitForm()">新增</el-button>
			<el-button size="medium" @click="visible = false">取消</el-button>
		</template>
	</sw-form-frame>
</template>

<script>
import lodash from 'lodash';
import selectIcon from './selectIcon';
import formMixins from '@/mixins/formMixins';
import { addedMenu, updateMenu } from '@/api/system/tbSysMenu';
export default {
	mixins: [formMixins],
	components: { selectIcon },
	props: {
		list: { type: Array, default: () => [] }
	},
	data() {
		return {
			formName: '菜单',
			isButton: '1',
			formRules: {
				pid: [{ required: true, message: '请选择上级菜单', trigger: 'change' }],
				name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
				del: [{ required: true, message: '请选择菜单类型', trigger: 'change' }],
				url: [{ required: true, message: '请输入菜单路径', trigger: 'blur' }]
			},
			treeSelectConfig: {
				filterable: true,
				placeholder: '选择菜单',
				style: { width: '100%' },
				defaultProps: { label: 'name', children: 'children' }
			}
		};
	},
	computed: {
		menuTreeList() {
			let allMenuList = this.$treeToFlat(lodash.cloneDeep(this.list), 'childList');
			let filteredMenus = allMenuList.filter((n) => n.del === '1' && n.menuLevel !== '4');
			return [{ id: '0', name: '顶级菜单', children: this.$flatToTree(filteredMenus) }];
		},
		isDisabled() {
			return this.isSub;
		},
		callFunction() {
			if (this.addOrEdit) {
				return eval(addedMenu);
			} else if (this.isSub) {
				return eval(addedMenu);
			} else {
				return eval(updateMenu);
			}
		},
		menuLevel() {
			console.log(this.allMenuList.find((n) => n.id === this.dataId));
		},
		submitParams() {
			let params = { ...this.formData };
			if (this.isSub) delete params.id;
			return params;
		}
	},
	methods: {
		formatFormParams() {}
	}
};
</script>
